/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {ListNode} head
 * @return {TreeNode}
 */
// 还可以利用中序遍历的特性
var sortedListToBST = function(head) {
    let temp = head
    let arr = []
    while(temp) {
        arr.push(temp.val)
        temp = temp.next
    }
    const dfs = function(left, right) {
        if(left > right) return null
        let mid = (left + right) >>> 1
        const node = new TreeNode(arr[mid])
        node.left = dfs(left, mid - 1)
        node.right = dfs(mid + 1, right)
        return node
    }
    return dfs(0, arr.length - 1)
};